class Promise {
    constructor(excutor) {
        this.promiseState = 'pending';
        this.promiseResult = undefined;
        this.callbacks = [];
        let resolve = (value) => {

            if (this.promiseState !== 'pending') return;

            this.promiseState = 'fulfilled';
            this.promiseResult = value;

            this.callbacks.forEach(item => {
                item.ok(this.promiseResult);
            })
        }
        let reject = (reason) => {
            if (this.promiseState !== 'pending') return;
            this.promiseState = 'rejected';
            this.promiseResult = reason;
            this.callbacks.forEach(item => {
                item.err(this.promiseResult);
            })
        }
        try {
            excutor(resolve, reject)
        } catch (err) {
            reject(err)
        }
    }
    then(resFn, rejFn) {
        return new Promise((res, rej) => {
    
            if (typeof resFn !== 'function') {
                resFn = val => val;
            }
    
            if (typeof rejFn !== 'function') {
                rejFn = rea => { throw rea };
            }
    
            let handleFn = (fnName) => {
                setTimeout(() => {
                    try {
                        let result = fnName(this.promiseResult);
                        //console.log(result);
                        if (result instanceof Promise) {
                            // promise
                            result.then(val => {
                                res(val)
                            }, rea => {
                                rej(rea)
                            })
                        } else {
                            // 非promise
                            res(result)
                        }
                    } catch (err) {
                        rej(err)
                    }
                }, 0);
            }
    
    
            // 同步修改状态
            if (this.promiseState === 'fulfilled') {
                handleFn(resFn)
            }
    
            if (this.promiseState === 'rejected') {
                handleFn(rejFn)
            }
    
            // 异步存储回调函数
            if (this.promiseState === 'pending') {
                this.callbacks.push({
                    ok: () => {
                        handleFn(resFn)
                    },
                    err: () => {
                        handleFn(rejFn)
                    }
                });
            }
        })
    }
    catch(rejFn) {
        return this.then(undefined, rejFn)
    }
    static resolve(val) {

        // val = 非promise
        // promise =看promise状态
        return new Promise((res, rej) => {
            if (val instanceof Promise) {
                // promise
                val.then(val => {
                    res(val)
                }, rea => {
                    rej(rea)
                })
            } else {
                // 非promise
                res(val)
            }
        })
    
    } 
    static reject(val) {
        return new Promise((res, rej) => {
            rej(val)
        })
    }
}